package com.dbuil.bleshoes.ui.activity;


import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AlertDialog;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.dbuil.bleshoes.MainActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.dbuil.bleshoes.R;
import com.dbuil.bleshoes.base.BaseActivity;
import com.dbuil.bleshoes.ui.view.GradeProgressView;
import com.dbuil.bleshoes.ui.view.NoiseboardView;
import com.dbuil.bleshoes.ui.view.WheelView;
import com.dbuil.bleshoes.utils.whiteutil.Battery;
import com.dbuil.bleshoes.utils.whiteutil.FcfrtAppBhUtils;
import com.dbuil.bleshoes.utils.MyCount;
import com.dbuil.bleshoes.utils.ScreenShotsUtil;
import com.dbuil.bleshoes.utils.ShareUtils;
import com.dbuil.bleshoes.utils.banner.BannerBean;
import com.dbuil.bleshoes.utils.banner.XBannerUtils;
import com.dbuil.bleshoes.utils.glides.GlideEngine;
import com.dbuil.bleshoes.utils.glides.GlideUtils;
import com.stx.xhb.androidx.XBanner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class ShareTestActivity extends BaseActivity {

    @BindView(R.id.home_xbanner)
    XBanner homeXbanner;
    @BindView(R.id.ll_points)
    LinearLayout llPoints;
    @BindView(R.id.bt_sendcode)
    Button btSendcode;
    @BindView(R.id.grade_progressview)
    GradeProgressView gradeProgressview;
    @BindView(R.id.btn_selectpicture)
    Button btnSelectpicture;
    @BindView(R.id.img_select)
    ImageView imgSelect;
    @BindView(R.id.noiseboardView)
    NoiseboardView noiseboardView;
    @BindView(R.id.wheel)
    WheelView wheel;
    private MyCount mc;

    ShareUtils shareUtils;
    String path;

    @Override
    public int getLayoutId() {
        return R.layout.activity_sharetest;
    }

    @Override
    public void initView() {
        super.initView();

    }

    @Override
    public void initData() {
        super.initData();
        noiseboardView.setMaxValue(100);
        noiseboardView.setRealTimeValue(66);
        initBanner();
        gradeProgressview.setProgress(60);

        //分享工具类
        shareUtils = new ShareUtils(this);
        //电池白名单
       // Battery.addWhite(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (!FcfrtAppBhUtils.isIgnoringBatteryOptimizations(this)){
                FcfrtAppBhUtils.requestIgnoreBatteryOptimizations(this);
            }
        }

        //选择器
        initSelect();
    }

    private void initSelect() {

        wheel.setOffset(2);// 对话框中当前项上面和下面的项数
        List<String> dataList=new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            dataList.add("dsadsad"+i);
        }
        wheel.setItems(dataList);// 设置数据源
        wheel.setSeletion(2);// 默认选中第三项

    }

    private void initBanner() {
        List<BannerBean> bannerdata = new ArrayList<>();
        for (String s : getUrl().subList(6, 11)) {
            bannerdata.add(new BannerBean(s, "", ""));
        }
        homeXbanner.setPointsIsVisible(false);//设置为false 不显示指示器
        XBannerUtils.indicatorBanner(this, homeXbanner, bannerdata, true, llPoints);
        XBannerUtils.setOnBannerItemClick((bannerBean, position) -> ActivityUtils.startActivity(HomeActivity.class));
    }


    /**
     * 请求发送验证码成功
     */
    public void sendCodeSuccess(View view) {

        if (mc == null) {
            mc = new MyCount(60100, 1000, this, btSendcode);
        }
        mc.start();
        btSendcode.setEnabled(true);


    }


    public void selectpicture(View view) {




//        PictureSelector.create(this)
//                .openCamera(PictureMimeType.ofImage())
//                .isCompress(true)//是否压缩
//                .cutOutQuality(70) //压缩质量
//                .minimumCompressSize(2000)// 小于2M的图片不压缩
//                .forResult(PictureConfig.REQUEST_CAMERA);

        // // 如果同时开启裁剪和压缩，则取压缩路径为准因为是先裁剪后压缩
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .theme(R.style.picture_WeChat_style)

               // .isOriginalImageControl(true)// 是否显示原图控制按钮，如果用户勾选了 压缩、裁剪功能将会失效

                .isEnableCrop(true)//是否裁剪 true or false
                .withAspectRatio(16,9)// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
               // .cropImageWideHigh(160,90)// 裁剪宽高比，设置如果大于图片本身宽高则无效 int
                .freeStyleCropEnabled(false)// 裁剪框是否可拖拽 true or false
                .showCropFrame(true)//是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                .rotateEnabled(false) // 裁剪是否可旋转图片 true or false
                .scaleEnabled(false)// 裁剪是否可放大缩小图片 true or false

                .isCompress(true)//是否压缩
                .compressQuality(70) //压缩质量
                .minimumCompressSize(500)// 小于500k的图片不压缩

                .imageEngine(GlideEngine.createGlideEngine()) // Please refer to the Demo GlideEngine.java
                .forResult(PictureConfig.CHOOSE_REQUEST);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mc != null) {
            mc.cancel();
            mc = null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        Battery.addWhite(this);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // onResult Callback
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    LogUtils.i("原图路径：" + selectList.get(0).getPath());
                    LogUtils.i("裁剪图片的路径：" +selectList.get(0).getCutPath());
                    LogUtils.i("压缩图片的路径：" +selectList.get(0).getCompressPath());
                    GlideUtils.loadImage(this, selectList.get(0).getCutPath(), imgSelect);
                    break;
                default:
                    break;
            }
        }

    }


    public void shareall(View view) {

        //本地资源文件分享
        String path = ScreenShotsUtil.saveResBitmap(this, R.drawable.login_bg);
        LogUtils.i("图片保存路径：" + path);
        shareUtils.shareImageAll(path);

        //shareUtils.shareText("我发现了一款好用的APP，快来体验吧 http://www.baidu.com","com.tencent.mm","com.tencent.mm.ui.tools.ShareImgUI");

    }

    public void qq(View view) {
        //截屏后分享
        Bitmap bitmap = ScreenShotsUtil.activityShot(this);
        String path = ScreenShotsUtil.saveBitmap(this, bitmap);
        LogUtils.i("图片保存路径：" + path);
        shareUtils.shareImageClassname(path, "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
    }

    public void friend(View view) {
        //截屏后分享
        Bitmap bitmap = ScreenShotsUtil.activityShot(this);
        String path = ScreenShotsUtil.saveBitmap(this, bitmap);
        LogUtils.i("图片保存路径：" + path);
        shareUtils.shareImageClassname(path, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
    }

    public void find(View view) {
        //截屏后分享
        Bitmap bitmap = ScreenShotsUtil.activityShot(this);
        String path = ScreenShotsUtil.saveBitmap(this, bitmap);
        LogUtils.i("图片保存路径：" + path);
        shareUtils.shareImageClassname(path, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
    }


}
